/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sisece.controlador;

import com.sisece.modelo.dato.Cliente;
import com.sisece.service.ClienteService;
import com.sisece.service.impl.ClienteServiceImpl;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;
import org.primefaces.context.RequestContext;

/**
 *
 * @author laptop-w8
 */
@ManagedBean
@RequestScoped
public class ClienteController implements Serializable {

    private String username;
    private String password;
    private boolean logueado;
    private Date date;
    private Cliente cliente;

    /**
     * Creates a new instance of LoginController
     */
    public ClienteController() {
    }

    public void login(ActionEvent actionEvent) {
        RequestContext requestContext = RequestContext.getCurrentInstance();
        FacesMessage mensaje;

//        if (egresado != null) {
//            perfilEgresado = es.obtenerPerfilEgresado(egresado);
//            logueado = true;
//            mensaje = new FacesMessage(FacesMessage.SEVERITY_INFO, "Bienvenido ", perfilEgresado.getDnmPercv());
//        } else {
//            logueado = false;
//            mensaje = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error en el ingreso", "credenciales invalidas");
//        }

//        FacesContext.getCurrentInstance().addMessage("forform09growl01", mensaje);
        requestContext.addCallbackParam("estaLogeado", logueado);
        if (isLogueado()) {
            System.out.println("SESION INICIADA::: USUARIO: " + username);
            String indexUsuario = "egresado/egresado.xhtml";
            requestContext.addCallbackParam("view", indexUsuario);
        } else {
            System.out.println("NO INICIO SESION " + username);
        }
    }

    public void logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
                .getExternalContext().getSession(false);
        session.invalidate();
        logueado = false;
        String url = "../index.xhtml";
        FacesContext fc = FacesContext.getCurrentInstance();
        try {
            fc.getExternalContext().redirect(url);
        } catch (IOException e) {
        }
        System.out.println("SESION TERMINADA");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isLogueado() {
        return logueado;
    }

    public void setLogueado(boolean logueado) {
        this.logueado = logueado;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

}
